<template>
  <view class="page padding-lr text-333 text-df">
    <view class=" margin-top-sm " v-for="(item, index) in combinationSpecList" :key="index">
      <view class="flex justify-between align-center height-100">
        <view class="spec-item-row-left">规格</view>
        <view class="spec-item-row-right">{{ item.key_name }}</view>
      </view>
	  <view class="bg-white padding-lr spec-item">
	  	<view class="spec-item-row flex justify-between align-center height-100 solid-bottom">
	  	  <view class="spec-item-row-left">划线价</view>
	  	  <view class="spec-item-row-right text-right flex justify-end align-center">
	  	    <input type="number" placeholder="0.00" v-model="item.market_price" placeholder-class="placeholder" />
	  	    <text class="">元</text>
	  	  </view>
	  	</view>
	  	<view class="spec-item-row flex justify-between align-center height-100 solid-bottom">
	  	  <view class="spec-item-row-left">价格</view>
	  	  <view class="spec-item-row-right text-right flex justify-end align-center">
	  	    <input type="number" placeholder="0.00" v-model="item.shop_price" placeholder-class="placeholder" />
	  	    <text class="">元</text>
	  	  </view>
	  	</view>
	  	<view class="spec-item-row flex justify-between align-center height-100 solid-bottom">
	  	  <view class="spec-item-row-left">会员价</view>
	  	  <view class="spec-item-row-right text-right flex justify-end align-center">
	  	    <input type="number" placeholder="0.00" v-model="item.member_price" placeholder-class="placeholder" />
	  	    <text class="">元</text>
	  	  </view>
	  	</view>
	  	<view class="spec-item-row flex justify-between align-center height-100 solid-bottom">
	  	  <view class="spec-item-row-left">成本价</view>
	  	  <view class="spec-item-row-right text-right flex justify-end align-center">
	  	    <input type="number" placeholder="0.00" v-model="item.cost_price" placeholder-class="placeholder" />
	  	    <text class="">元</text>
	  	  </view>
	  	</view>
	  	<view class="spec-item-row flex justify-between align-center height-100">
	  	  <view class="spec-item-row-left">库存</view>
	  	  <view class="spec-item-row-right text-right flex justify-end align-center">
	  	    <input type="number" placeholder="0" v-model="item.stock" placeholder-class="placeholder" />
	  	  </view>
	  	</view>
	  </view>
    </view>
    <view class="footer"><view class="btn" @click="addSpecSave">保存</view></view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      combinationSpecList: []
    };
  },
  onLoad(options) {
    let combinationSpecList = JSON.parse(uni.getStorageSync('combinationSpecList'));
    this.combinationSpecList = combinationSpecList;
    console.log('combinationSpecList', combinationSpecList);
  },
  methods: {
	  onBackPress(e) {
		console.log(e);
		if(e.from == 'backbutton'){
			this.$util.showModal({
				title:'提示',
				content:'规格数据尚未保存,确认退出吗?',
				confirmColor:'#FC6497',
				success:res=>{
					console.log(res);
					if(res.confirm){
						uni.navigateBack({
							delta:1
						})
					}
				},
				fail:err=>{
					console.log(err);
				}
			})
			return true;//阻止默认返回行为
		}
	},
    addSpecSave() {
		if(this.checkGoodsInfo()) return this.$message.info(this.checkGoodsInfo());
		console.log('来了老弟')
		// uni.$emit('combinationSpecList',JSON.stringify(this.combinationSpecList));
		uni.setStorageSync('combinationSpecList', JSON.stringify(this.combinationSpecList));
		uni.navigateBack({
			 delta: 1
		});
	},
    // 验证
    checkGoodsInfo() {
        let err = '';
        for (let i = 0; i < this.combinationSpecList.length; i++) {
          let val = this.combinationSpecList[i];
          let sku_name = this.combinationSpecList[i].item;
          // if (!val.market_price.trim()) {
          //   err = '请输入' + sku_name + '商品划线价';
          //   break;
          // } else {
          //   if (val.market_price <= 0) {
          //     err = sku_name + '商品划线价需要大于0';
          //     break;
          //   }
          // }
          if (!val.shop_price.trim()) {
            err = '请输入' + sku_name + '商品本店价';
            break;
          } else {
            if (val.shop_price <= 0) {
              err = sku_name + '商品本店价需要大于0';
              break;
            }
          }
          // if (!val.member_price.trim()) {
          //   err = '请输入' + sku_name + '商品会员价';
          //   break;
          // } else {
          //   if (val.member_price <= 0) {
          //     err = sku_name + '商品会员价需要大于0';
          //     break;
          //   }
          // }
          if (!val.cost_price.trim()) {
            err = '请输入' + sku_name + '商品成本价';
            break;
          } else {
            if (val.cost_price <= 0) {
              err = sku_name + '商品成本价需要大于0';
              break;
            }
          }

          if (!val.stock) {
            err = '请输入' + sku_name + '商品库存';
            break;
          } else {
            if (val.stock <= 0) {
              err = sku_name + '商品库存需要大于0';
              break;
            }
          }
        }
        return err;
      
    }
  }
};
</script>

<style lang="scss" scoped>
.page {
  padding-bottom: calc(300rpx + constant(safe-area-inset-bottom));
  padding-bottom: calc(300rpx + env(safe-area-inset-bottom));
}
.height-100 {
  height: 100rpx;
}
.placeholder {
  font-size: 28rpx;
  color: #bfbfbf;
}
.spec-item{
	border-radius: 16rpx;
}
.spec-item-row-right{
	
	input{
		padding-right: 20rpx;
	}
}
.footer {
  position: fixed;
  bottom: 0;
  left: 0;
  padding: 30rpx;
  padding-bottom: calc(30rpx + constant(safe-area-inset-bottom));
  padding-bottom: calc(30rpx + env(safe-area-inset-bottom));
  width: 100%;
  background: white;
  .btn {
    width: 100%;
    height: 90rpx;
    line-height: 90rpx;
    text-align: center;
    color: white;
    background: $bgtheme;
    border-radius: 45rpx;
  }
}
</style>
